package com.treehouse.url.loader;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import com.treehouse.logging.Logger;
import com.treehouse.pornstar.Pornstar;
import com.treehouse.scene.Scene;
import com.treehouse.url.HTMLParser;
import com.treehouse.url.farmer.IURLFarmer;
import com.treehouse.url.farmer.URLFarmer;

public class TrailerLoader implements ISceneLoader{
	private String rootDirectory;
	private IURLFarmer farmer;
	private HTMLParser parser;

	public TrailerLoader(String root, IURLFarmer farmer2, HTMLParser parser) {
		this.rootDirectory = root;
		this.farmer = farmer2;
		this.parser = parser;
		File f = new File(this.rootDirectory);
		if (!f.isDirectory()) {
			f.mkdir();
		}
	}

	public void loadScenesForPornstar(Pornstar pornstar) {
		try{
		String directory = this.rootDirectory + "\\" + pornstar.getName();
		File f = new File(directory);
		if (!f.isDirectory()) {
			f.mkdir();
		}
		System.out.println("Loader Scenes for " + pornstar.getName() + "..");
		pornstar.setScenes(this.farmer.lookupSceneList(pornstar));
		for (Scene scene : pornstar.getScenes()) {
			System.out.println("Loading " + scene.getTitle() + ".. ");
			// Because MP4 works for everything fine
			scene.setFormat(".mp4");
			URL url = scene.getUrl();
			BufferedReader in = new BufferedReader(new InputStreamReader(
					url.openStream()));
			String inputLine = "";
			while ((inputLine = in.readLine()) != null) {
				if (inputLine.length() > 40 && inputLine.contains("\"720\":")) {
					int start = inputLine.indexOf("\"720\":");
					inputLine = inputLine.substring(start);
					String splitInput[] = inputLine.split("\"");
					String link = "";
					for (String s : splitInput) {
						if (s.length() > 6 && s.contains("http")) {
							link = s;
							break;
						}
					}
					link = parser.decodeHTML(link);

					URL vidURL = new URL(link);
					this.loadScene(vidURL, directory + "\\" + scene.getTitle() + " Trailer"
							+ scene.getFormat());
					System.out.println("done!");
					break;
				}
			}
			in.close();
		}
		}catch(IOException e){
			Logger.err(e.getMessage());
		}
	}

	public void loadScene(URL url, String path) {
		try {
			path = path.replaceAll("\\?", "");
			URLConnection connection = url.openConnection();
			InputStream in = connection.getInputStream();
			int contentLength = connection.getContentLength();

			ByteArrayOutputStream tmpOut;
			if (contentLength != -1) {
				tmpOut = new ByteArrayOutputStream(contentLength);
			} else {
				tmpOut = new ByteArrayOutputStream(16384 * 200000);
			}

			byte[] buf = new byte[512];
			while (true) {
				int len = in.read(buf);
				if (len == -1) {
					break;
				}
				tmpOut.write(buf, 0, len);
			}
			in.close();
			tmpOut.close();

			byte[] array = tmpOut.toByteArray();

			FileOutputStream fos = new FileOutputStream(path);
			fos.write(array);
			fos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public String getRootDirectory() {
		return rootDirectory;
	}

	public void setRootDirectory(String rootDirectory) {
		this.rootDirectory = rootDirectory;
	}

	@Override
	public void loadSpecificSceneForPornstar(Scene scene, Pornstar pornstar) {
		// TODO Auto-generated method stub
		
	}
}
